package com.gitee.wsl.logger

import platform.Foundation.NSLog
import platform.Foundation.NSThread

class AppleLogPrinter(
    private val maxTagLength: Int = DEFAULT_MAX_TAG_LENGTH,
) : LogPrinter {

    override fun log(level: LogLevel, tag: String, messageAny: Any?, throwable: Throwable?) {
        val appleTag = tag.takeIf { it.length > maxTagLength }
            ?.substring(0, maxTagLength)
            ?: tag

        val thread = NSThread.currentThread().run { "$name:$threadPriority" }
        val composed = "($thread) $messageAny"
        val finalMessage = throwable?.let {
            "$composed\n${it.message}"
        } ?: composed

        NSLog("[$level] ($appleTag) $finalMessage")
    }

    companion object{
        val INSTANCE = AppleLogPrinter()

        internal const val DEFAULT_MAX_TAG_LENGTH = 23
    }

}